package org.subside.MQ.rabbitmq.RoutingModel;

import com.rabbitmq.client.*;
import org.subside.MQ.rabbitmq.config.ConnectionUtil;

import java.io.IOException;

/**
 * @Author : zhangbo
 * @data : 2024/4/14
 * @description :
 */
public class ConsumerRoutingRight {
    private static final String EXCHANGE_NAME = "exchange_routing";
    private static final String QUEUE_RIGHT = "rightQueue";
    private static final String ROUTINGKEY_RIGHT = "rightKey";

    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");
            channel.queueDeclare(QUEUE_RIGHT, false, false, true, null);
            //将交换器与队列通过路由键绑定
            channel.queueBind(QUEUE_RIGHT, EXCHANGE_NAME, ROUTINGKEY_RIGHT);
            DeliverCallback callback = (String consumerTag, Delivery message) -> {
                String msg = new String(message.getBody(), "UTF-8");
                System.out.println(msg);
            };
            System.out.println("消费者Right正在监听。。。。");
            channel.basicConsume(QUEUE_RIGHT, true, callback, (consumerTag) -> {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
